package com.logic.landseaserver.service.impl;

import com.logic.landseaserver.common.config.ScheduleConfiguration;
import com.logic.landseaserver.common.util.CommonUtils;
import com.logic.landseaserver.common.util.ConstantChar;
import com.logic.landseaserver.service.IScheduleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Junny on 2017/8/1.
 */
@Service
public class ScheduleServiceImpl implements IScheduleService{

    private static final Logger log = LoggerFactory.getLogger(ScheduleServiceImpl.class);
    @Autowired
    private ScheduleConfiguration scheduleConfig;

    @Override
    public boolean cuteSchedule() {
        boolean cuteFlag= true;
        String cuteIp = scheduleConfig.getHostIp();
        String cuteEnv = scheduleConfig.getCuteEnv();
        String localIp = CommonUtils.getLocalLanIpAddress();
        log.info("cuteIp is :"+cuteIp+"  cuteEnv is: "+cuteEnv+" localIp is :"+localIp);
        //如果是生成环境，则只有符合ip白名单的才能执行
        if(cuteIp!=null && cuteEnv!=null){
            log.info("env is prd");
            if(cuteEnv.equals(ConstantChar.PRD_ENV) && cuteIp.equals(localIp)){
                cuteFlag = true;
            }else{
                cuteFlag = false;
            }
        }else{
            log.info("env is others");
            cuteFlag = true;
        }
        return cuteFlag;
    }

    @Override
    public boolean sendMsgFlag() {
        boolean cuteFlag= false;
        String cuteIp = scheduleConfig.getHostIp();
        String cuteEnv = scheduleConfig.getCuteEnv();
        String localIp = CommonUtils.getLocalLanIpAddress();
        //如果是生成环境，则发送短信
        if(cuteEnv!=null && cuteIp!=null){
            if(cuteEnv.equals(ConstantChar.PRD_ENV) && cuteIp.equals(localIp)){
                log.info("cuteIp is :"+cuteIp+"  cuteEnv is: "+cuteEnv+" localIp is :"+localIp);
                cuteFlag = true;
            }else{
                cuteFlag = false;
            }
        }else{
            log.info("env is others");
            cuteFlag = false;
        }
        return cuteFlag;
    }
}
